import math
import pandas as pd

def temperature_rise_1(Iu, Iv, Iw, frequency):
    rpm = 0.0
    torque = 0.0
    Im = 0.0
    k = [0.0, 0.0]
    k0 = 0.0
    P_in = 0.0
    C_loss = 0.0
    loss = 0.0
    cos = 0.0
    R = 2.35
    power = 0.0
    i = 0

    Im = (Iu + Iv + Iw) / 3
    rpm = frequency * 60
    torque = Im * 0.44420 + 0.0558
    power = rpm * torque / 9.55
    cos = 0.000001 * pow(rpm, 2) - 0.0017 * rpm - 0.0443 * pow(torque, 2) + 0.0264 * torque + 1.3779
    P_in = math.sqrt(3) * rpm / 3000 * Im * 220 * cos
    C_loss = pow(Im, 2) * R
    loss = P_in - power + C_loss
    k[0] = 0.428987895 * loss
    R = R * (1 + 0.00393 * k[0])
    C_loss = pow(Im, 2) * R
    loss = P_in - power + C_loss
    k[1] = 0.428987895 * loss

    while abs((k[0] - k[1]) / k[0]) > 0.1:
        if i == 0:
            R = R * (1 + 0.00393 * (k[1] - k[0]))
            C_loss = pow(Im, 2) * R
            loss = P_in - power + C_loss
            k[0] = k[1]
            k[1] = 0.428987895 * loss
        if i == 1:
            R = R * (1 + 0.00393 * (k[0] - k[1]))
            C_loss = pow(Im, 2) * R
            loss = P_in - power + C_loss
            k[1] = 0.428987895 * loss
        i = -(i - 1)

    k0 = k[1]  # 热稳定时温升
    return k0


def temperature_rise_route(data_path):
    suffix = data_path.split(".")[1]
    jug_list = []
    #如果输入文件是xlsx格式的。
    if suffix == "xlsx":
        data = pd.read_excel(data_path)
        for i in range(len(data)):
            u = data.loc[i,"U_phase_Current"]
            v = data.loc[i,"V_phase_Current"]
            w = data.loc[i,"W_phase_Current"]
            f = data.loc[i,"frequency"]
            k = temperature_rise_1(u, v, w, f)
            # print(k)

            if (k > 100) or (k < 25):
                jug = 1
                jug_list.append([jug])
            else:
                jug =0
                jug_list.append([jug])

    if suffix == "txt":

        with open(data_path, "r", encoding='utf-8') as file:
            for line in file:
            #筛选并存储算法输入数据
                parts = line.strip().split(';')
                values = {}
                for part in parts:
                    key, value = part.split(':')
                    values[key] = float(value)
                    # print(values)
                    u = values.get('U_phase_Current', 0.0)
                    v = values.get('V_phase_Current', 0.0)
                    w = values.get('W_phase_Current', 0.0)
                    f = values.get('frequency', 0.0)
                    # print(u, v, w, f)
                    k = temperature_rise_1(u, v, w, f)
                    # print(k)

                if (k > 100) or (k < 25):
                    jug = 1
                    jug_list.append([jug])
                else:
                    jug = 0
                    jug_list.append([jug])

    return jug_list


if __name__ == "__main__":
    # 读取文件路径
    data_path = 'input_temperature.xlsx'
    jugs = temperature_rise_route(data_path)
    # 输出 状态(0-正常;1-异常)
    print('状态(0-正常;1-异常):', jugs)



